package com.cskaoyan.controller.wx;

import com.cskaoyan.bean.BasePageInfo;
import com.cskaoyan.bean.vo.BaseRespVo;
import com.cskaoyan.bean.MarketTopic;
import com.cskaoyan.bean.param.CommonData;
import com.cskaoyan.bean.vo.topic.ReadTopicVO;
import com.cskaoyan.bean.vo.topic.WXTopicListVO;
import com.cskaoyan.service.admin.MarketTopicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author RXC
 * @date 2022-6-28
 */
@RestController
@RequestMapping("wx/topic")
public class WXTopicController {

    // 复用后台的service
    @Autowired
    MarketTopicService marketTopicService;

    /**
     * 显示所有专题，只需要考虑分页
     * ?page=1&limit=10, 这里由于后台需要读取sort，所有需要手动添加sort
     */
    @RequestMapping("list")
    public BaseRespVo list(BasePageInfo basePageInfo) {
        basePageInfo.setSort("add_time");
        CommonData<MarketTopic> commonData = marketTopicService.list(null, null,null, basePageInfo);
        return BaseRespVo.ok(commonData);
    }

    /**
     * 进入专题显示详情，对应后台的topic的read
     * @param id
     */
    @RequestMapping("detail")
    public BaseRespVo detail(Integer id) {
        ReadTopicVO readTopicVO = marketTopicService.read(id);
        if (readTopicVO == null) {
            return BaseRespVo.invalidData("该专题已不存在");
        }
        // 变量名不同所以需要手摇车
        WXTopicListVO wxTopicListVO = new WXTopicListVO();
        wxTopicListVO.setTopic(readTopicVO.getTopic());
        wxTopicListVO.setGoods(readTopicVO.getGoodsList());

        return BaseRespVo.ok(wxTopicListVO);
    }

    /**
     * 进入专题详情，复用后台service的list方法，但要自己设置分页
     * limit设为4，page设为1，sort设置为add_time
     * @param id
     * @return
     */
    @RequestMapping("related")
    public BaseRespVo related(Integer id) {
        // 手动设置分页信息
        BasePageInfo basePageInfo = new BasePageInfo();
        basePageInfo.setPage(1);
        basePageInfo.setLimit(4);
        basePageInfo.setSort("add_time");
        // 复用后台service的list方法
        CommonData<MarketTopic> data = marketTopicService.list(null, null, id, basePageInfo);
        return BaseRespVo.ok(data);
    }

}
